package com.example.demo.web.manage.service;

import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.easy.query.solon.annotation.Db;
import com.example.demo.web.manage.convert.UserDeptConvert;
import com.example.demo.web.manage.dto.UserDeptDto;
import com.example.demo.web.manage.model.UserDeptEntity;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;
import org.noear.solon.data.annotation.Tran;
import org.noear.solon.data.tran.TranPolicy;

/**
 * @author airhead
 */
@Component
@Slf4j
public class UserDeptService {
  @Db("db1")
  private EasyEntityQuery entityQuery;

  @Tran
  public void addUserDept(UserDeptDto userDeptDto) {
    UserDeptEntity userDept = UserDeptConvert.INSTANCE.convert(userDeptDto);
    entityQuery.insertable(userDept).executeRows(true);
  }

  @Tran
  public Boolean rollbackUserDept(UserDeptDto userDeptDto) {
    UserDeptEntity userDept = UserDeptConvert.INSTANCE.convert(userDeptDto);
    entityQuery.insertable(userDept).executeRows(true);

    throw new RuntimeException("不能添加子表");
  }

  @Tran(policy = TranPolicy.requires_new)
  public void addUserDept1(UserDeptDto userDeptDto) {
    UserDeptEntity userDept = UserDeptConvert.INSTANCE.convert(userDeptDto);
    entityQuery.insertable(userDept).executeRows(true);
  }

  @Tran(policy = TranPolicy.nested)
  public Boolean rollbackUserDept1(UserDeptDto userDeptDto) {
    UserDeptEntity userDept = UserDeptConvert.INSTANCE.convert(userDeptDto);
    entityQuery.insertable(userDept).executeRows(true);

    throw new RuntimeException("不能添加子表");
  }
}
